/**
 * 
 */
package org.pump.client.codec;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import org.apache.mina.core.buffer.IoBuffer;
import org.pump.client.main.MyProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liudong03
 *
 */
public class DumpBinlogCommand extends CommandPack{
	private static final Logger mylog = LoggerFactory.getLogger(DumpBinlogCommand.class);
	long binlog_position;
	byte[] binlog_flags = new byte[2];
	long server_id;
	String binlog_file;
	
	public DumpBinlogCommand()
	{
		this.command = 0x12;
		this.len = 11;
	}
	public DumpBinlogCommand(MyProperty conf)
	{
		this(conf.binlogPosition,conf.serverid,conf.binlogFile);
	}
	/**
	 * 
	 */
	public DumpBinlogCommand(long binlog_position,long server_id,String binlog_file) {
		// TODO Auto-generated constructor stub
		this.command = 0x12;
		this.len = 11;
		this.binlog_position = binlog_position;
		this.server_id = server_id;
		this.setBinlog_file(binlog_file);
		
	}
	
	@Override
	public boolean writebuffer(IoBuffer out)
	{
		out.put(command);
		out.putUnsignedInt(binlog_position);
		out.put(binlog_flags);
		out.putUnsignedInt(server_id);
		if(this.binlog_file != null && this.binlog_file.length() > 0)
		{
			try {
				out.put(binlog_file.getBytes("US-ASCII"));
			} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
				mylog.error("encod the string fail",e);
				return false;
			}
		}
		return true;
	}

	/**
	 * @param binlog_file the binlog_file to set
	 */
	public void setBinlog_file(String binlog_file) {
		this.binlog_file = binlog_file;
		if(this.binlog_file != null && this.binlog_file.length() > 0)
		{
			try {
				this.len += this.binlog_file.getBytes("US-ASCII").length;
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				mylog.info("encode fail",e);
			}
		}
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "DumpBinlogCommand [binlog_position=" + binlog_position
				+ ", binlog_flags=" + Arrays.toString(binlog_flags)
				+ ", server_id=" + server_id + ", binlog_file=" + binlog_file
				+ ", command=" + command + ", len=" + len + "]";
	}
	
	
	
}
